package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
    /**
     * 查询地址列表  条件查询
     * @param addressBook
     * @return
     */
    @Override
    public List<AddressBook> list(AddressBook addressBook){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        // 动态设置条件
        queryWrapper.eq(AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.eq(
                addressBook.getIsDefault()!=null,
                AddressBook::getIsDefault,
                addressBook.getIsDefault()
        );
        List<AddressBook> addressBookList = super.list(queryWrapper);
        return addressBookList;
    }

    /**
     * 保存地址信息
     * @param addressBook
     */
    @Override
    public void saveAddress(AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        super.save(addressBook);
    }

    /**
     * 根据Id 查询地址
     * @param id
     * @return
     */
    public AddressBook getById(Long id){
        AddressBook addressBook = super.getById(id);
        return addressBook;
    }

    /**
     * 根据Id 修改地址信息
     * @param addressBook
     */
    @Override
    public void updateAddressById(AddressBook addressBook){
        super.updateById(addressBook);
    }

    /**
     * 修改默认地址
     * @param addressBook
     */
    @Override
    public void setDefault(AddressBook addressBook){
        Long uId = BaseContext.getCurrentId();
        Long AddressId = addressBook.getId();
        // 修改原有默认地址
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, uId);
        queryWrapper.eq(AddressBook::getIsDefault, 1);
        AddressBook addressBook1 = super.getOne(queryWrapper);
        if (addressBook1!=null){
            addressBook1.setIsDefault(0);
            super.updateById(addressBook1);
        }
        // 设置默认地址
        LambdaQueryWrapper<AddressBook> addressBookQueryWrapper = new LambdaQueryWrapper<>();
        addressBookQueryWrapper.eq(AddressBook::getUserId, uId);
        addressBookQueryWrapper.eq(AddressBook::getId, AddressId);
        AddressBook book = super.getOne(addressBookQueryWrapper);
        book.setIsDefault(1);
        super.updateById(book);
    }

    /**
     * 根据Id删除地址信息
     * @param id
     */
    @Override
    public void deleteById(Long id){
        super.removeById(id);
    }


}
